<?php
/**
 *	  商户管理服务层
 *	  by Ouyoung
 *
 */

class supplier_service extends service {		
	protected $count;
    protected $pages;
	
	public function __construct() {
		$this->supplier_db = $this->load->table('supplier/supplier');
	}
    
	public function get_lists($params){
		$lists = $result = array();
		if($params['label'] == 1 || $params['label'] == FALSE){
			$params['status'] = array('neq',-1);
            $list_sql = $this->supplier_db->status($params['status']);
            
			if($params['keyword']){
				$keyword = trim($params['keyword']);
				$keyword_sql = $this->keyword_search($keyword);
				$count = $this->supplier_db->status($params['status'])->where($keyword_sql)->count();
				$result = $list_sql->where($keyword_sql)->order('id desc')->getField('id',TRUE);
			}else{
				$result = $list_sql->page($params['page'])->limit($params['limit'])->order('id desc')->getField('id',TRUE);
				$count = $this->supplier_db->status($params['status'])->count();
			}
			if(empty($result)){
				$this->error = lang('_select_not_exist_');
				return FALSE;
			}else{
    			foreach ($result as $key => $value) {
    				$lists['lists'][] = $this->detail($value);
    			}
            }            
        }else{
			$sqlmap = array();
			switch ($params['label']) {
				case '2':
					$sqlmap[config("DB_PREFIX").'supplier.status'] = 0;
					break;
				case '3':
					$sqlmap[config("DB_PREFIX").'supplier.status'] = 1;
					break;
				case '4':
					$sqlmap[config("DB_PREFIX").'supplier.status'] = -1;
					break;
				default:
					break;
			}
              
			if(!empty($params['keyword'])){
				 $sqlmap['company|'.config("DB_PREFIX").'supplier.supplier_name|recommender|contacts'] = array("LIKE", '%'.$params["keyword"].'%');
			}
            
            $_out_supplier =  $this->supplier_db->where($sqlmap)->page($params['page'])->limit($params['limit'])->getField('id',TRUE);
            $count = $this->supplier_db->where($sqlmap)->count();
			foreach ($_out_supplier as $key => $value) {
				$lists['lists'][] = $this->detail($value);
			}
        }
        $lists['count'] = ($count > 0) ? $count : 0;
        return $lists;        
    }
    
    /**
     * [detail 获取商家信息]
     * @param  [type]  $id    [id]
     * @return [array]         [商家数据]
     */
    public function detail($id){
    	$id = (int) $id;
		if ($id < 1) {
			$this->error = lang('_param_error_');
			return FALSE;
		}
		$supplier_info = array();
		$supplier_info = $this->supplier_db->detail($id)->output();
		if (!$supplier_info) {
			$this->error = lang('supplier/supplier_not_exist');
			return FALSE;
		}
		return $supplier_info;
    }
    
	/**
	 * [keyword_search 商家关键字查询]
	 * @param  [str] $keyword [关键字:公司名、商家简称、负责人、推荐人]
	 * @return [type]          [description]
	 */
	 public function keyword_search($keyword){
        $_nameids = (array)$this->supplier_db->where(array('company'=>array('LIKE','%'.$keyword.'%')))->getField('id',TRUE);
        $where['supplier_name|recommender|contacts'] = array("LIKE", '%'.$keyword.'%');
        $_sellersids = (array)$this->supplier_db->where($where)->distinct(TRUE)->getField('id',TRUE);
        $result_ids = array_unique(array_merge($_nameids,$_sellersids));
		$sqlmap = array();
		$sqlmap['id'] = array('IN',$result_ids);
        return $sqlmap;
    }
    
	/**
     * 查询单个信息
     * @param int $id 主键ID
     * @param string $field 被查询字段
     * @return mixed
     */
    public function fetch_by_id($id, $field = NULL) {
        $r = $this->supplier_db->find($id);
        if(!$r) return FALSE;
        return ($field !== NULL) ? $r[$field] : $r;
    }

	/**
	 * [更新商户]
	 * @param array $data 数据
	 * @param bool $valid 是否M验证
	 * @return bool
	 */
	public function save($data, $valid = FALSE) {
		if($valid == TRUE){
			$data = $this->supplier_db->create($data);
			$result = $this->supplier_db->add($data);
		}else{
			$result = $this->supplier_db->update($data);
		}
		return $result;
	}
    
	/**
	 * [ajax_del 删除商户，在商户列表里删除只改变状态，在回收站里删除直接删除]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public function ajax_del($params){
		$id = $params['id'];
		$data = $sqlmap = $map = array();
		if($id){
			$sqlmap['id'] = $map['supplier_id'] = array('IN',$id);
			$data['status'] = -1;
			$result = $this->supplier_db->where($sqlmap)->save($data);
            //删除商家时，同时禁用商家登录账号
            //$this->supplier_user->where($map)->save(array('islock'=>1)));
			if(!$result){
				$this->error = lang('_operation_fail_');
				return FALSE;
			}
			return TRUE;
		}else{
			$this->error = lang('_param_error_');
			return FALSE;
		}
	}
    
	/**
	 * [ajax_recover 批量恢复商家]
	 * @param  [array] $id [要恢复的商家id]
	 * @return [type]     [description]
	 */
	public function ajax_recover($id){
		$id = (array)$id;
		if(empty($id)) {
			$this->error = lang('_param_error_');
			return FALSE;
		}
		$data = array();
			$sqlmap['id'] = $map['supplier_id'] = array('IN',$id);
			$data['status'] = 0;
			$result = $this->supplier_db->where($sqlmap)->save($data);
		if(!$result){
			$this->error = lang('_operation_fail_');
		}
		return $result;
	}

	/**
	 * [更新商户名称]
	 * @param array $params 数据
	 * @return bool
	 */
	public function ajax_save_name($params){
		if((int)$params['id'] < 1){
			$this->error = lang('_param_error_');
			return FALSE;
		}
		$data = array();
		$data['supplier_name'] = $params['supplier_name'];
		$result = $this->supplier_db->where(array('id'=>$params['id']))->save($data);
		if(!$result){
    		$this->error = lang('_operation_fail_');
    	}
    	return $result;
	}

	/**
	 * [API调用商户接口]
	 * @param array $params 数据
	 * @return array//
	 */
    public function ajax_supplier($keyword){ 
        $sqlmap = array();
        if ($keyword){
            $sqlmap = array('supplier_name'=>array('LIKE','%'.trim($keyword).'%'));
        }
        $result = $this->supplier_db->where($sqlmap)->getField('id,supplier_name',TRUE);
        if (!$result){
            $this->error = lang('_operation_fail_');
        }
        return $result;
    }

    
	/**
	 * [API调用商户接口]
	 * @param array $params 数据
	 * @return array//
	 */
	public function get_suppliers($sqlmap = array()){
		$result = $this->supplier_db->where($sqlmap = array())->getField('id,company,supplier_name');
		if(!$result){
    		$this->error = lang('_operation_fail_');
    	}
    	return $result;
	}
    
    
}
